package com.fdb.efp.console.executor.impl;

import com.fdb.efp.console.common.constance.FlowConstance;
import com.fdb.efp.console.common.vo.EngineVO;
import com.fdb.efp.console.service.vo.BizFlowTaskParamVO;
import com.fdb.efp.console.service.vo.InstFlowTaskVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * 页面插件执行器
 * 
 * @author Cytus_
 * @since 2018年5月14日 下午2:22:09
 * @version 1.0
 *
 */
@Service("pagePluginTaskExecutor")
public class PagePluginTaskExecutor extends AbstractPluginTaskExecutor {
	
	private static Logger logger = LoggerFactory.getLogger(PagePluginTaskExecutor.class);

	public EngineVO doTask(EngineVO evo, InstFlowTaskVO task)  throws Exception {
		logger.info("节点实例号:"+ evo.getNodeInstanceId() +", 任务编号："+ evo.getTaskId() +"执行当前任务页面插件开始......");
		
		if (FlowConstance.TASK_STATUS_END.equals(task.getTaskState())) {
			evo.setTaskExecuteStatus(FlowConstance.TASK_STATUS_END);
			logger.info("节点实例号:"+ evo.getNodeInstanceId() +", 任务编号："+ evo.getTaskId() +"当前任务页面插件已执行!");
			return evo;
		}
		
		//如果当前任务是暂停，再次进入该方法则表示该页面插件任务已经处理完成，即将任务状态修改为完成，否则为第一次进入，将任务状态修改为暂停
		evo.setTaskExecuteStatus(FlowConstance.TASK_STATUS_PAUSE.equals(task.getTaskState()) ? FlowConstance.TASK_STATUS_END : FlowConstance.TASK_STATUS_PAUSE);
		evo = setDefaultParam2EVO(evo, task);
		logger.info("节点实例号:"+ evo.getNodeInstanceId() +", 任务编号："+ evo.getTaskId() +"执行当前任务页面插件结束......");
		return evo;
	}
	
	
	protected EngineVO setDefaultParam2EVO(EngineVO evo, InstFlowTaskVO task) {
	    List<BizFlowTaskParamVO> listParams = getTaskParamsByTaskId(task.getTaskId());
        List<BizFlowTaskParamVO> params = null;
        
        if (FlowConstance.TASK_STATUS_PAUSE.equals(task.getTaskState()) || FlowConstance.TASK_STATUS_EXCEPTION.equals(task.getTaskState())) {
            params = listParams.parallelStream().filter(s -> FlowConstance.PLUGIN_OUT_PARAM.equals(s.getBizParamIo())).collect(Collectors.toList());
        } else {
            params = listParams.parallelStream().filter(s -> FlowConstance.PLUGIN_IN_PARAM.equals(s.getBizParamIo())).collect(Collectors.toList());
        }

        evo = this.dealDefaultParam(evo, params);
        return evo;
	}
	
}
